package com.dajie.pt.service.base;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import com.dajie.pt.entity.base.EntityDao;


/**
 * 
 * @author panbing.wu
 * 
 * @param <E>
 * @param <PK>
 */
public abstract class BaseServiceImpl<E, PK extends Serializable> implements BaseService<E, PK>
{

	protected Logger log = LoggerFactory.getLogger(getClass());
	protected String currentEmail;

	public abstract EntityDao<E, PK> getEntityDao();

	public List<E> getByIds(final String[] ids)
	{
		return getEntityDao().findBy("ids", StringUtils.join(ids, ","));
	}

	public E get(final PK id)
	{
		return (E) getEntityDao().getById(id);
	}

	public List<E> findAll()
	{
		return getEntityDao().findAll();
	}

	public Object save(final E entity)
	{
		return getEntityDao().save(entity);
	}

	public void saveWithLog(Object logInfo, final E entity)
	{
		getEntityDao().saveWithLog(logInfo, entity);
	}

	public void update(final E entity)
	{
		getEntityDao().update(entity);
	}

	public void updateWithLog(Object logInfo, final E entity)
	{
		getEntityDao().updateWithLog(logInfo, entity);
	}

	public void saveOrUpdate(final E entity)
	{
		getEntityDao().saveOrUpdate(entity);
	}

	public void saveOrUpdateWithLog(Object logInfo, final E entity)
	{
		getEntityDao().saveOrUpdateWithLog(logInfo, entity);
	}

	public void removeById(final PK id)
	{
		getEntityDao().deleteById(id);
	}

	public void removeByIds(final String[] ids)
	{
		final String idsStr = StringUtils.join(ids, ',');
		removeBy("ids", idsStr);
	}

	public void removeByIdWithLog(Object logInfo, final PK id)
	{
		getEntityDao().deleteByWithLog(logInfo, "id", id);
	}

	public void removeByIdsWithLog(Object logInfo, final String[] ids)
	{
		final String idsStr = StringUtils.join(ids, ',');
		getEntityDao().deleteByWithLog(logInfo, "ids", idsStr);
	}

	public boolean isPropertyUnique(final String property, final String orgValue, final String newValue)
	{
		return getEntityDao().isPropertyUnique(property, orgValue, newValue);
	}

	public boolean isUnique(final E entity, final String uniquePropertyNames)
	{
		return getEntityDao().isUnique(entity, uniquePropertyNames);
	}

	protected void removeBy(final Object... params)
	{
		getEntityDao().deleteBy(params);
	}

	public List<E> findBy(final Object... params)
	{
		return find(map(params));
	}

	protected List<E> find(final Map map)
	{
		return getEntityDao().findByMap(map);
	}

	public E findUniqueBy(final Object... params)
	{
		return (E) getEntityDao().findUniqueBy(params);
	}

	/**
	 * 根据参数构造map，参数必须为偶数个，依次为key1，value1，key2，value2…….
	 * 
	 * @param datas
	 *            参数列表
	 * @return 构造出的map
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	protected Map map(final Object... datas)
	{
		Assert.isTrue(datas.length % 2 == 0, "参数必须为偶数个");
		final Map map = new HashMap();
		for (int i = 0; i < datas.length; i += 2)
		{
			map.put(datas[i], datas[i + 1]);
		}
		return map;
	}

}
